import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_elf/utils/toast_utils.dart';

class AnimationHomeWidget extends StatefulWidget {
  @override
  _AnimationHomeWidgetState createState() => _AnimationHomeWidgetState();
}

class _AnimationHomeWidgetState extends State with SingleTickerProviderStateMixin {
  AnimationController animationController;
  Animation degOneTranslationAnimation;
  Animation rotationAnimation;
  bool open = false;

  double getRadiansFromDegree(double degree) {
    double unitRadian = 57.295779513;
    return degree / unitRadian;
  }

  @override
  void initState() {
    //WidgetsBinding.instance.addPostFrameCallback((_) => showOverlay());
    animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 250));
    degOneTranslationAnimation = TweenSequence([
      TweenSequenceItem(tween: Tween(begin: 0.0, end: 1.2), weight: 75.0),
      TweenSequenceItem(tween: Tween(begin: 1.2, end: 1.0), weight: 25.0),
    ]).animate(animationController);
    rotationAnimation = Tween(begin: 180.0, end: 0.0)
        .animate(CurvedAnimation(parent: animationController, curve: Curves.easeOut));
    super.initState();
    animationController.addListener(() {
      setState(() {});
    });
//    degOneTranslationAnimation.addStatusListener((AnimationStatus status) {
//      switch (status) {
//        case AnimationStatus.dismissed:
//          break;
//        case AnimationStatus.forward:
//          open = true;
//          break;
//        case AnimationStatus.reverse:
//          open = false;
//          break;
//        case AnimationStatus.completed:
//          break;
//      }
//    });
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: EdgeInsets.only(left: 100,top: 100),
        width: 80,
        height: 80,
        child: Stack(
          children: [
//          SlideTransition(
//              child: CircularButton(
//                width: 70,
//                height: 70,
//                icon: 'images/ic_get_follow.png',
//                onClick: () {
//                  print('click');
//                  Navigator.push(context, MaterialPageRoute(builder: (_) => FollowAddPage()));
//                },
//              ),
//              position: Tween(
//                      begin: Offset(0.0, 0.0),
//                      end: Offset.fromDirection(
//                          getRadiansFromDegree(0), degOneTranslationAnimation.value * 1.5))
//                  .animate(animationController)),
            Transform.translate(
              offset: Offset.fromDirection(
                  getRadiansFromDegree(315), 0.45 * 100),
              child: GestureDetector(
                onTap: (){
                  print('1111111111111111111');
                },
                child: Transform(
                  transform: Matrix4.rotationZ(getRadiansFromDegree(rotationAnimation.value))
                    ..scale(degOneTranslationAnimation.value),
                  alignment: Alignment.center,
                  child: RaisedButton(
                    child: Text('222222222222222222222222222222222222222222222222222222'),
                    onPressed: (){
                      showToast('====');
                    },
                  ),
//                child: CircularButton(
//                  width: 70,
//                  height: 70,
//                  onClick: () {
//                    print('点击1');
//                    //Navigator.push(context, MaterialPageRoute(builder: (_) => LikesPage()));
//                  },
//                ),
                ),
              ),
            ),
            Transform.translate(
              transformHitTests: true,
              offset: Offset.fromDirection(
                  getRadiansFromDegree(225), degOneTranslationAnimation.value * 100),
              child: Transform(
                transformHitTests: true,
                transform: Matrix4.rotationZ(getRadiansFromDegree(rotationAnimation.value))
                  ..scale(degOneTranslationAnimation.value),
                alignment: Alignment.center,
                child: RaisedButton(
                  child: Text('11111111111111'),
                  onPressed: (){
                    showToast('====');
                  },
                ),
//                child: CircularButton(
//                  width: 70,
//                  height: 70,
//                  onClick: () {
//                    print('点击2');
////                  Navigator.push(context, MaterialPageRoute(builder: (_) => FollowAddPage()));
//                  },
//                ),
              ),
            ),
            RaisedButton(
              child: Text(open ? '关' : '开'),
              onPressed: (){
                print('点击');
                if (animationController.isCompleted) {
                  animationController.reverse();
                  open = false;
                } else {
                  animationController.forward();
                  open = true;
                }
              },
            ),
//            CircularButton(
//              width: 80,
//              height: 80,
//              onClick: () {
//
//              },
//            ),
          ],
        ),
      ),
    );
  }
}

class CircularButton extends StatelessWidget {
  final double width;
  final double height;
  final Color color;
  final VoidCallback onClick;

  CircularButton({this.color, this.width, this.height, this.onClick});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: color, shape: BoxShape.circle),
      width: width,
      height: height,
      child: GestureDetector(
        child: RaisedButton(
          child: Text('========'),
          onPressed: onClick,
        ),
      ),
    );
  }
}
